// customerPackage/pages/addCustomer/addCustomer.js

const app = getApp()

import {
  pycode
} from '../../../utils/pinyin'


var bmap = require('../../../lib/bmap-wx'); //bmap-wx.js文件存放路径
var BMap = new bmap.BMapWX({
  ak: 'd2KjPsoWTSGWv83Nif3XD7CuRCLxCrFr' //百度地图密钥
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    customer: {
      customerSex: '',
      customerName: '',
      customerShopName: '',
      customerTel: '',
      customerAddress: '',
      customerNote: '',
      customerLong: '',
      customerLat: '',
      customerPosition: '正在定位中...',

    },
    shopPhoto: '', //门店图片
    fileList: [], //
  },


  // 上传头像
  upHandle() {
    var that = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      camera: 'back',
      success(res) {
        console.log(res.tempFiles)
        that.setData({
          shopPhoto: res.tempFiles[0],
          fileList: res.tempFiles,
        })
        console.log(that.data.shopPhoto);
        console.log(that.data.fileList[0].tempFilePath);
      }
    })
  },
  // 示例函数，获取字符串中每个汉字的首字母



  saveHandle() {
    var url = app.globalData.url + "customer/addCustomer",
      that = this;
    wx.showLoading({
      title: '',
    })
    console.log(that.data.customer);
    var customer = this.data.customer;

    if (this.data.fileList.length == 0) {
      wx.showToast({
        title: '请上传门店图片',
        icon: 'none'
      })
      return
    }
    if (customer.customerShopName == '') {
      wx.showToast({
        title: '请输入门店名称',
        icon: 'none'
      })
      return
    }
    if (customer.customerName == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (customer.customerTel == '') {
      wx.showToast({
        title: '请输入联系方式',
        icon: 'none'
      })
      return
    }
    if (customer.customerSex == '') {
      wx.showToast({
        title: '请选择姓名',
        icon: 'none'
      })
      return
    }
    if (customer.customerAddress == '') {
      wx.showToast({
        title: '请输入收货地址',
        icon: 'none'
      })
      return
    }
    if (customer.customerLat == '') {
      wx.showToast({
        title: '位置错误，请重新定位',
        icon: 'none'
      })
      return
    }



    var firstWord = this.getF(that.data.customer.customerShopName);
    console.log(firstWord);
    console.log(that.data.fileList[0].tempFilePath);
    // return
    wx.uploadFile({
      filePath: that.data.fileList[0].tempFilePath,
      name: 'file',
      url: url,
      formData: {
        customerSex: that.data.customer.customerSex,
        customerName: that.data.customer.customerName,
        customerShopName: that.data.customer.customerShopName,
        customerTel: that.data.customer.customerTel,
        customerAddress: that.data.customer.customerAddress,
        customerNote: that.data.customer.customerNote,
        customerLong: that.data.customer.customerLong,
        customerLat: that.data.customer.customerLat,
        userId: app.globalData.userId,
        customerPosition: that.data.customer.customerPosition,
        companyId: app.globalData.companyId,
        customerFirstWord: firstWord,
      },
      success(res) {
        console.log(res);
        var result = JSON.parse(res.data)
        if (result.flag) {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '添加客户成功',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          })
        }
      }
    })

  },


  getMyInfo() {
    var that = this,
      customer = this.data.customer;

    console.log(1)
    wx.showLoading({
      title: '定位中...',
    })
    BMap.regeocoding({
      success(data) {
        console.log(data);
        var str = data.wxMarkerData[0].address + data.wxMarkerData[0].desc
        customer.customerLat = data.wxMarkerData[0].latitude
        customer.customerLong = data.wxMarkerData[0].longitude
        customer.customerPosition = str
        that.setData({
          customer
        })
        wx.hideLoading()
      },
      fail(res) {
        wx.showToast({
          title: '定位失败,请重试',
          icon: 'none'
        })
        console.log(res);
        wx.hideLoading()
      }
    })
  },
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    console.log(e);
    this.setData({
      [str]: val
    })
  },

  onChange(event) {
    var str = event.currentTarget.dataset.flag
    this.setData({
      [str]: event.detail
    });
    console.log(this.data);
  },


  getF(str) {
    return pycode[str.toUpperCase().substr(0, 1)]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.getMyInfo()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})